package com.piggy.settlement.sdk.client.identityaudit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.PiggyIdentityAuditConstants;
import com.piggy.settlement.sdk.util.PiggyVerifyUtil;

/**
 * @author ：daxia
 * @description：人工认证审核相关接口
 * @modified By：
 * @version: 1.0$
 */
public class PiggyIdentityAuditApiClient {

    static PiggyIdentityAuditApiClient piggyIdentityAuditApiClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggyIdentityAuditApiClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggyIdentityAuditApiClient getInstanse(String appSecret, String domain) {
        if (piggyIdentityAuditApiClient == null) {
            piggyIdentityAuditApiClient =
                    new PiggyIdentityAuditApiClient(appSecret, domain);
        }

        return piggyIdentityAuditApiClient;
    }


    /**
     * 根据用户信息上传相关数据提交审核
     *
     * @param auditVo 参数实体
     * @return
     */
    public JSONObject auditApply(Object auditVo) {

        String response = "";
        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, auditVo);
            response = HttpRequest.post(domain + PiggyIdentityAuditConstants.AUDIT_APPLY)
                    .header("sig", signStr)
                    .header(Header.CONTENT_TYPE, "multipart/form-data")
                    .form(BeanUtil.beanToMap(auditVo))
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("上报小猪人工认证审核数据失败:");
            System.out.println(response);
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 根据上传接口反馈的业务流水号查询审核结果
     *
     * @param auditResultVo 参数实体
     * @return
     */
    public JSONObject auditResult(Object auditResultVo) {

        String response = "";
        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, auditResultVo);
            response = HttpRequest.post(domain + PiggyIdentityAuditConstants.AUDIT_RESULT)
                    .header("sig", signStr)
                    .form(BeanUtil.beanToMap(auditResultVo))
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("获取小猪人工认证审核结果错误:");
            System.out.println(response);
            e.printStackTrace();
            return null;
        }

    }

}
